﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainDrive.Core.Interface;
using Esb.Repository.RepoImplements;

namespace Esb.Domain.Factories
{
    /// <summary>
    /// 工厂基类
    /// </summary>
    /// <typeparam name="DomainObject"></typeparam>
    /// <typeparam name="PrimaryKey"></typeparam>
    public abstract class FactoryBase<DomainObject, PrimaryKey> where DomainObject : IAggregateRoot<PrimaryKey>, new()
    {
        public FactoryBase(string connectionStringName)
        {
            Repository = new MongoRepo<DomainObject, PrimaryKey>();
        }

        /// <summary>
        /// 仓储实例
        /// </summary>
        public IRepository<DomainObject, PrimaryKey> Repository;

        public DomainObject Add(DomainObject obj)
        {
            Repository.Add(obj);
            return obj;
        }

        public bool Delete(PrimaryKey id)
        {
            DomainObject obj = Repository.GetById(id);
            Repository.Remove(obj);
            return true;
        }

        /// <summary>
        /// 根据Id获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DomainObject GetById(PrimaryKey id)
        {
            DomainObject result = Repository.GetById(id);
            return result;
        }
    }
}
